#include "Timer16Bit.h"


unsigned long int Timer16Bit::getFrequencyCTC(
					unsigned long int CLK_IO,
					unsigned long int prescaler,
					unsigned long int OCR_Value
					)
{
	double value = 0;
	value = CLK_IO / ( 2.0 * prescaler * (1+OCR_Value));
	unsigned long int f = (unsigned long int) value;
	return value;
}

unsigned long int Timer16Bit::getFrequencyFastPWM(
				unsigned long int CLK_IO,
				unsigned long int prescaler,
				unsigned long int TOP
				)
{
	double value = 0;
	value = CLK_IO / (prescaler * 1.0 * (1 + TOP));
	unsigned long int f = (unsigned long int) value;
	return f;
}

unsigned long int Timer16Bit::getFrequencyPhaseCorrectPWM(
				unsigned long int CLK_IO,
				unsigned long int prescaler,
				unsigned long int TOP
				)
{
	double value = 0;
	value = CLK_IO / (1.0 * 2 * prescaler * TOP);
	unsigned long int f = (unsigned long int) value;
	return f;
}

unsigned long int Timer16Bit::getFrequencyPhaseFrequencyCorrectPWM(
			unsigned long int  CLK_IO,
			unsigned long int prescaler,
			unsigned long int TOP
			)
{
	double value = 0;
	value = CLK_IO / (1.0 * 2 * prescaler * TOP);
	unsigned long int f = (unsigned long int) value;
	return f;
}




void Timer16Bit::setCompareUnitAOutputMode(unsigned char flag)
{
	switch(flag)
	{
		case NormalPortOperation_OC1A_DisconnectTimer16:
			TCCR1A = SetBit(TCCR1A, COM1A0, 0);
			TCCR1A = SetBit(TCCR1A, COM1A1, 0);
			break;
		case Toggle_OC1A_onCompareTimer16:
			TCCR1A = SetBit(TCCR1A, COM1A0, 1);
			TCCR1A = SetBit(TCCR1A, COM1A1, 0);
			break;
		case Clear_OC1A_onCompareTimer16:
			TCCR1A = SetBit(TCCR1A, COM1A0, 0);
			TCCR1A = SetBit(TCCR1A, COM1A1, 1);
			break;
		case Set_OC1A_onCompareTimer16:
			TCCR1A = SetBit(TCCR1A, COM1A0, 1);
			TCCR1A = SetBit(TCCR1A, COM1A1, 1);
			break;
		default:
			TCCR1A = SetBit(TCCR1A, COM1A0, 0);
			TCCR1A = SetBit(TCCR1A, COM1A1, 0);
			break;
	}	
}

void Timer16Bit::setCompareUnitBOutputMode(unsigned char flag)
{
	switch(flag)
	{
		case NormalPortOperation_OC1A_DisconnectTimer16:
			TCCR1A = SetBit(TCCR1A, COM1B0, 0);
			TCCR1A = SetBit(TCCR1A, COM1B1, 0);
			break;
		case Toggle_OC1A_onCompareTimer16:
			TCCR1A = SetBit(TCCR1A, COM1B0, 1);
			TCCR1A = SetBit(TCCR1A, COM1B1, 0);
			break;
		case Clear_OC1A_onCompareTimer16:
			TCCR1A = SetBit(TCCR1A, COM1B0, 0);
			TCCR1A = SetBit(TCCR1A, COM1B1, 1);
			break;
		case Set_OC1A_onCompareTimer16:
			TCCR1A = SetBit(TCCR1A, COM1B0, 1);
			TCCR1A = SetBit(TCCR1A, COM1B1, 1);
			break;
		default:
			TCCR1A = SetBit(TCCR1A, COM1B0, 0);
			TCCR1A = SetBit(TCCR1A, COM1B1, 0);
			break;
	}	

}

void Timer16Bit::setForceOutputCompareA(unsigned char flag)
{
	TCCR1A = SetBit(TCCR1A, FOC1A, flag);
}

void Timer16Bit::setForceOutputCompareB(unsigned char flag)
{
	TCCR1A = SetBit(TCCR1A, FOC1B, flag);
}


void Timer16Bit::setWaveformGenerationMode(unsigned char flag)
{

	switch(flag)
	{
		case NormalWaveformGenerationTimer16:
			TCCR1A = SetBit(TCCR1A, WGM10, 0);
			TCCR1A = SetBit(TCCR1A, WGM11, 0);
			TCCR1B = SetBit(TCCR1B, WGM12, 0);
			TCCR1B = SetBit(TCCR1B, WGM13, 0);
			break;
		case PWM_PhaseCorrect_8Bit_Timer16:
			TCCR1A = SetBit(TCCR1A, WGM10, 1);
			TCCR1A = SetBit(TCCR1A, WGM11, 0);
			TCCR1B = SetBit(TCCR1B, WGM12, 0);
			TCCR1B = SetBit(TCCR1B, WGM13, 0);
			break;
		case PWM_PhaseCorrect_9Bit_Timer16:
			TCCR1A = SetBit(TCCR1A, WGM10, 0);
			TCCR1A = SetBit(TCCR1A, WGM11, 1);
			TCCR1B = SetBit(TCCR1B, WGM12, 0);
			TCCR1B = SetBit(TCCR1B, WGM13, 0);
			break;
		case PWM_PhaseCorrect_10Bit_Timer16:
			TCCR1A = SetBit(TCCR1A, WGM10, 1);
			TCCR1A = SetBit(TCCR1A, WGM11, 1);
			TCCR1B = SetBit(TCCR1B, WGM12, 0);
			TCCR1B = SetBit(TCCR1B, WGM13, 0);
			break;
		case CTC_Timer16:
			TCCR1A = SetBit(TCCR1A, WGM10, 0);
			TCCR1A = SetBit(TCCR1A, WGM11, 0);
			TCCR1B = SetBit(TCCR1B, WGM12, 1);
			TCCR1B = SetBit(TCCR1B, WGM13, 0);
			break;
		case FastPWM_8Bit_Timer16:
			TCCR1A = SetBit(TCCR1A, WGM10, 1);
			TCCR1A = SetBit(TCCR1A, WGM11, 0);
			TCCR1B = SetBit(TCCR1B, WGM12, 1);
			TCCR1B = SetBit(TCCR1B, WGM13, 0);
			break;
		case FastPWM_9Bit_Timer16:
			TCCR1A = SetBit(TCCR1A, WGM10, 0);
			TCCR1A = SetBit(TCCR1A, WGM11, 1);
			TCCR1B = SetBit(TCCR1B, WGM12, 1);
			TCCR1B = SetBit(TCCR1B, WGM13, 0);
			break;
		case FastPWM_10Bit_Timer16:
			TCCR1A = SetBit(TCCR1A, WGM10, 1);
			TCCR1A = SetBit(TCCR1A, WGM11, 1);
			TCCR1B = SetBit(TCCR1B, WGM12, 1);
			TCCR1B = SetBit(TCCR1B, WGM13, 0);
			break;
		case PWM_PhaseAndFrequencyCorrect1_Timer16:
			TCCR1A = SetBit(TCCR1A, WGM10, 0);
			TCCR1A = SetBit(TCCR1A, WGM11, 0);
			TCCR1B = SetBit(TCCR1B, WGM12, 0);
			TCCR1B = SetBit(TCCR1B, WGM13, 1);
			break;
		case PWM_PhaseAndFrequencyCorrect2_Timer16:
			TCCR1A = SetBit(TCCR1A, WGM10, 1);
			TCCR1A = SetBit(TCCR1A, WGM11, 0);
			TCCR1B = SetBit(TCCR1B, WGM12, 0);
			TCCR1B = SetBit(TCCR1B, WGM13, 1);
			break;
		case PWM_PhaseCorrect1_Timer16:
			TCCR1A = SetBit(TCCR1A, WGM10, 0);
			TCCR1A = SetBit(TCCR1A, WGM11, 1);
			TCCR1B = SetBit(TCCR1B, WGM12, 0);
			TCCR1B = SetBit(TCCR1B, WGM13, 1);
			break;
		case PWM_PhaseCorrect2_Timer16:
			TCCR1A = SetBit(TCCR1A, WGM10, 1);
			TCCR1A = SetBit(TCCR1A, WGM11, 1);
			TCCR1B = SetBit(TCCR1B, WGM12, 0);
			TCCR1B = SetBit(TCCR1B, WGM13, 1);
			break;
		case CTC_ICR1_2_Timer16:
			TCCR1A = SetBit(TCCR1A, WGM10, 0);
			TCCR1A = SetBit(TCCR1A, WGM11, 0);
			TCCR1B = SetBit(TCCR1B, WGM12, 1);
			TCCR1B = SetBit(TCCR1B, WGM13, 1);
			break;
		case Reserved_Timer16:
			TCCR1A = SetBit(TCCR1A, WGM10, 1);
			TCCR1A = SetBit(TCCR1A, WGM11, 0);
			TCCR1B = SetBit(TCCR1B, WGM12, 1);
			TCCR1B = SetBit(TCCR1B, WGM13, 1);
			break;
		case FastPWM_1_Timer16:
			TCCR1A = SetBit(TCCR1A, WGM10, 0);
			TCCR1A = SetBit(TCCR1A, WGM11, 1);
			TCCR1B = SetBit(TCCR1B, WGM12, 1);
			TCCR1B = SetBit(TCCR1B, WGM13, 1);
			break;
		case FastPWM_2_Timer16:
			TCCR1A = SetBit(TCCR1A, WGM10, 1);
			TCCR1A = SetBit(TCCR1A, WGM11, 1);
			TCCR1B = SetBit(TCCR1B, WGM12, 1);
			TCCR1B = SetBit(TCCR1B, WGM13, 1);
			break;
		default:
			TCCR1A = SetBit(TCCR1A, WGM10, 0);
			TCCR1A = SetBit(TCCR1A, WGM11, 0);
			TCCR1B = SetBit(TCCR1B, WGM12, 0);
			TCCR1B = SetBit(TCCR1B, WGM13, 0);
			break;
	}
}


